package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.Article;
import cn.itsource.domain.ArticleType;
import cn.itsource.query.ArticleQuery;
import cn.itsource.service.IArticleService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
public class ArticleController {
    @Autowired
    private IArticleService articleTypeService;

    /**
     * 增加/修改
     * @param
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="article:add", name = "文章增加/修改",descs = "文章增加/修改")
    public R addORupdate(@RequestBody Article article){
        try {
            if (article.getId() == null){
                articleTypeService.add(article);
            }else {
                articleTypeService.update(article);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="article:del", name = "文章删除",descs = "文章删除")
    public R del(@PathVariable("id") Long id){
        try {
            articleTypeService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="article:delList", name = "文章批量删除",descs = "文章批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            articleTypeService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(sn="article:get", name = "文章获取单个",descs = "文章获取单个")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(articleTypeService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(sn="article:getAll", name = "文章查询所有",descs = "文章查询所有")
    public R getAll(){
        try {
            return R.ok(articleTypeService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="article:pageQuery", name = "文章分页查询",descs = "文章分页查询")
    public PageInfo<Article> pageQuery(@RequestBody ArticleQuery query){
        return articleTypeService.pageQuery(query);
    }

    /**
     * 获取分类树
     * @return
     */
    @PatchMapping("/typeTrees")
    @ApiOperation(value = "分类树")
    @PreAuthorize(sn="article:typeTree", name = "文章分类树",descs = "文章分类树")
    public R<List<ArticleType>> articleTypeTree(){
        try {
            return R.ok(articleTypeService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 查询相关文章
     */

    @ApiOperation(value = "相关文章")
    @GetMapping("/correlationArticles/{id}")
    @PreAuthorize(sn="article:correlationArticles", name = "相关文章",descs = "相关文章")
    public List<Article> getCorrelationArticles(@PathVariable("id")Long id){
        return articleTypeService.getCorrelationArticles(id);
    }

    /**
     * 文章分类页面文章
     */
    @GetMapping("/articleTypes/{id}")
    @ApiOperation(value = "文章分类页面文章")
    @PreAuthorize(sn="article:articleTypes", name = "文章分类页面文章",descs = "文章分类页面文章")
    public List<Article> getarticleTypes(@PathVariable("id")Long id){
        return articleTypeService.getarticleTypes(id);
    }
}
